package easymusic.baseutility;

import java.io.File;
import easymusic.baseinterface.*;
import easymusic.baseinterface.IBasicService.ServiceID;
import java.io.FilenameFilter;
import java.util.ArrayList;

import com.easymusic.EasyMusicMainActivity;

import easymusic.baseutility.EasyMusicAppService;
import easymusic.baseutility.EasyMusicAppService.EasyMusicAppServiceBinder;
import android.app.Application;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.os.Process;

public class EasyMusicApplication extends Application {
	
	// for storing the medium's path
	//private ArrayList<String> m_arrayMedium = new ArrayList<String>();
	private int m_iCurrentPlayedMediaIndex = -1;
	private EasyMusicMainActivity m_activity;
	private IBasicService m_service;
	private String m_strCurrentTitle;
	private int m_iDuration;
	
	private ServiceConnection m_serviceConnection = new ServiceConnection(){
		@Override
		public void onServiceConnected(ComponentName arg0, IBinder arg1) {	
			EasyMusicAppServiceBinder binder = (EasyMusicAppServiceBinder)arg1;
			m_service = binder.getService();
			m_activity.setPlayService(m_service.getService(IBasicService.ServiceID.MediaService));
			m_activity.refreshMedium();
			m_activity.refreshListView();
		}

		@Override
		public void onServiceDisconnected(ComponentName arg0) {
		}
	}; 
	
	@Override
	public void onCreate (){
		Intent intent = new Intent(this, EasyMusicAppService.class);
		startService(intent);
		bindService(intent, m_serviceConnection, Context.BIND_AUTO_CREATE);
	}
	
	public IBasicService getService(ServiceID id){
		if (null == m_service){
			Log.d("jacklog", "jacklog: in getService of application");
			return null;
		}
		return m_service.getService(id);
	}
	
	public void setCurrentTitle(String strCurrentTitle){
		m_strCurrentTitle = strCurrentTitle;
	}
	
	public String getCurrentTitle(){
		return m_strCurrentTitle;
	}
	
	public void setDuration(int iDuration){
		m_iDuration = iDuration;
	}
	
	public int getDuration(){
		return m_iDuration;
	}
	
	
	/*public void addMediaPath(String strPath){
		// it is refreshed so the current index is invalid
		m_iCurrentPlayedMediaIndex = -1; 
		m_arrayMedium.add(strPath);
	}
	
	public String getMediaPath(int iIndex){
		return m_arrayMedium.get(iIndex);
	}
	
	public int getNumberOfMedium(){
		return m_arrayMedium.size();
	}
	
	public void clearAllMedium(){
		m_arrayMedium.clear();
	}*/
	
	/*private FilenameFilter m_filter = new FilenameFilter(){

		@Override
		public boolean accept(File dir, String strName) {
			File f = new File(dir.getPath() + "/" + strName);
			if (f.isDirectory()){
				return true;
			}
			return strName.endsWith(".mp3");
		}
	};
	
	public void searchMedium(String strPath, boolean bIsRecursive){
		File file = new File(strPath);
		File[] files = file.listFiles(m_filter);
		if (null == files){
			return;
		}
		
		for (File f: files){
			if (f.isDirectory() && bIsRecursive){
				searchMedium(f.getPath(), bIsRecursive);
				continue;
			}
			if (f.isFile()){
				m_arrayMedium.add(f.getPath());
			}
		}
	}*/

	public void setActivity(EasyMusicMainActivity activity) {
		m_activity = activity;
	}

	public void setCurrentMediaIndex(int position) {
		m_iCurrentPlayedMediaIndex = position;
	}

	/*public String getNextMediaPath() {
		// the list has been refreshed, so cannot get next song
		if (-1 == m_iCurrentPlayedMediaIndex){
			return null;
		}
		++m_iCurrentPlayedMediaIndex;
		if (m_iCurrentPlayedMediaIndex < m_arrayMedium.size()){
			return m_arrayMedium.get(m_iCurrentPlayedMediaIndex);
		}
		return null;
	}*/
	
	/*public String calculateTimeLeft(int iLeftTime){
		int iTime = iLeftTime / 1000;
		int iMinutes = iTime / 60;
		int iSeconds = iTime % 60;
		StringBuffer buffer = new StringBuffer();
		buffer.append(iMinutes);
		buffer.append(":");
		buffer.append(iSeconds);
		return buffer.toString();		
	}*/

	public View findViewById(int id) {
		return m_activity.findViewById(id);
	}

	public void setUIToStop() {
		m_activity.setUIToStop();
	}

	/*public String getSomeMediaPath() {		
		if (m_arrayMedium.size() > 0){
			if (-1 == m_iCurrentPlayedMediaIndex){
				return m_arrayMedium.get(0);
			}else{
				if (m_iCurrentPlayedMediaIndex < m_arrayMedium.size()){
					return m_arrayMedium.get(m_iCurrentPlayedMediaIndex);
				}else{
					m_iCurrentPlayedMediaIndex = 0;
					return m_arrayMedium.get(m_iCurrentPlayedMediaIndex);
				}
			}
		}
		
		return null;
	}*/

	public String buildNewTitle(String strTimeLeft, String title) {
		return strTimeLeft + " " + title;
	}

	public void shutDown() {
		m_service.stopService();
		Process.killProcess(Process.myPid());
	}
}






























